Imports System.Net
Imports System.IO

Public Class frmIPlist
	Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

	Public Sub New()
		MyBase.New()

		'This call is required by the Windows Form Designer.
		InitializeComponent()

		'Add any initialization after the InitializeComponent() call

	End Sub

	'Form overrides dispose to clean up the component list.
	Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
		If disposing Then
			If Not (components Is Nothing) Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(disposing)
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	Friend WithEvents lstIp As System.Windows.Forms.ListView
	Friend WithEvents Ip As System.Windows.Forms.ColumnHeader
	Friend WithEvents PlayerName As System.Windows.Forms.ColumnHeader
  Friend WithEvents btnOK As System.Windows.Forms.Button
  Friend WithEvents PortNo As System.Windows.Forms.ColumnHeader
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.lstIp = New System.Windows.Forms.ListView
    Me.Ip = New System.Windows.Forms.ColumnHeader
    Me.PlayerName = New System.Windows.Forms.ColumnHeader
    Me.btnOK = New System.Windows.Forms.Button
    Me.PortNo = New System.Windows.Forms.ColumnHeader
    Me.SuspendLayout()
    '
    'lstIp
    '
    Me.lstIp.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.Ip, Me.PlayerName, Me.PortNo})
    Me.lstIp.Location = New System.Drawing.Point(16, 16)
    Me.lstIp.Name = "lstIp"
    Me.lstIp.Size = New System.Drawing.Size(376, 248)
    Me.lstIp.TabIndex = 0
    Me.lstIp.View = System.Windows.Forms.View.Details
    '
    'Ip
    '
    Me.Ip.Text = "Host Name"
    Me.Ip.Width = 182
    '
    'PlayerName
    '
    Me.PlayerName.Text = "IP Address"
    Me.PlayerName.Width = 172
    '
    'btnOK
    '
    Me.btnOK.Location = New System.Drawing.Point(96, 272)
    Me.btnOK.Name = "btnOK"
    Me.btnOK.Size = New System.Drawing.Size(216, 32)
    Me.btnOK.TabIndex = 1
    Me.btnOK.Text = "Select Other Player"
    '
    'PortNo
    '
    Me.PortNo.Text = "Port No"
    Me.PortNo.Width = 150
    '
    'frmIPlist
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(408, 311)
    Me.Controls.Add(Me.btnOK)
    Me.Controls.Add(Me.lstIp)
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
    Me.MaximizeBox = False
    Me.MinimizeBox = False
    Me.Name = "frmIPlist"
    Me.Text = "Vikaumit ChessMania Server list"
    Me.ResumeLayout(False)

  End Sub

#End Region


  Dim t_IPSelected As String
  Dim t_IPPortSelected As Integer
  'TO Play on local lan Enable Localhost entry here and in Modfunctions.vb file
  Dim OnlineData As String = BaseURL & "online/"


  'Function to Get the List of Host Games
	Sub GetHostedGameList()
        Dim ips() As String
        Dim re As HttpWebRequest = CType(WebRequest.Create(OnlineData), HttpWebRequest)
        re.UserAgent = HTTPReferrer
        Dim res As HttpWebResponse = CType(re.GetResponse(), HttpWebResponse)
        Dim receiveStream As Stream = res.GetResponseStream()
        ' Pipes the stream to a higher level stream reader with the required encoding format. 

        Dim sr As New StreamReader(receiveStream)
        Dim str As String

        Dim i As Integer = 0
    While sr.Peek > -1

      str = sr.ReadLine
      ips = str.Split(CChar("|"))
      lstIp.Items.Add(ips(0))
      lstIp.Items(i).SubItems.Add(ips(1))
      lstIp.Items(i).SubItems.Add(ips(2))
      i += 1
    End While
    If i = 0 Then
      btnOK.Enabled = False
    End If
    sr.Close()
    res.Close()
    End Sub


  Public ReadOnly Property IPAddr() As String
    Get
      Return Me.t_IPSelected
    End Get
  End Property

  Public ReadOnly Property IPPort() As Integer
    Get
      Return Me.t_IPPortSelected
    End Get
  End Property

  Private Sub frmIPlist_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    GetHostedGameList()
  End Sub

  Private Sub lstIp_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstIp.DoubleClick, btnOK.Click
    If lstIp.SelectedItems(0).SubItems(1).Text <> "" Then
      Me.t_IPSelected = lstIp.SelectedItems(0).SubItems(1).Text
      t_IPPortSelected = CInt(lstIp.SelectedItems(0).SubItems(2).Text)
    Else
      Me.t_IPSelected = "0.0.0.0"
      t_IPPortSelected = 1000
    End If
    Me.Hide()
  End Sub

End Class